<?php
 /*****************************************************************************
 * updateInvoice.php                                                          *
 * Last Modified: 2007-06-06                                                  *
 *                                                                            *
 * updateInvoice.php is the driver file used to update an invoice record with *
 * new data passed by POST from editInvoice.php.                              *
 *                                                                            *
 * BBG_Billing, a PHP application using MySQL for creating and maintaining a  *
 * contacts and invoices database.                                            *
 *                                                                            *
 * @copyright Copyright (C) 2007, Bugs Bee Gone Computer Services             *
 * @owner     Daniel Barnett (Bugs Bee Gone Computer Services)                *
 * @author    Daniel Barnett <dbarnett@bugsbeegone.com>                       *
 * @website   http://www.bugsbeegone.com                                      *
 * @license   http://www.gnu.org/licenses/gpl.html GNU General Public License *
 * @package   BBG_Billing                                                     *
 * @name      updateInvoice.php                                               *
 * @version   1.0.0                                                           *
 * @uses      Jpmaster77's Login Script (Written by: Jpmaster77, 2004-08-19)  *
 *             ->(http://www.evolt.org/PHP-Login-System-with-Admin-Features/) *
 *                                                                            *
 * This file is part of the "BBG_Billing" PHP application.                    *
 *                                                                            *
 * BBG_Billing is free software; you can redistribute it and/or modify        *
 * it under the terms of the GNU General Public License as published by       *
 * the Free Software Foundation; either version 2 of the License, or          *
 * (at your option) any later version.                                        *
 *                                                                            *
 * BBG_Billing is distributed in the hope that it will be useful,             *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU General Public License for more details.                               *
 *                                                                            *
 * You should have received a copy of the GNU General Public License          *
 * along with BBG_Billing; if not, write to the Free Software                 *
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
 *****************************************************************************/


    // Require session.php for user access control
    require "include/session.php";

    // If user is not logged in, redirect to main.php
    if(!$session->logged_in){
        header("Location: main.php");
        exit();
    }

    // Check if invoiceNum was passed by POST, if it was...
    if ($_POST['invoiceNum'] != "") {
        // Define data variables
        $invoiceNum = $_POST['invoiceNum'];
        $date = $_POST['date'];
        $customerID = $_POST['customerID'];

        $item1Qty = $_POST['item1Qty'];
        $item2Qty = $_POST['item2Qty'];
        $item3Qty = $_POST['item3Qty'];
        $item4Qty = $_POST['item4Qty'];
        $item5Qty = $_POST['item5Qty'];
        $item6Qty = $_POST['item6Qty'];
        $item7Qty = $_POST['item7Qty'];
        $item8Qty = $_POST['item8Qty'];
        $item9Qty = $_POST['item9Qty'];
        $item10Qty = $_POST['item10Qty'];

        $item1Name = $_POST['item1Name'];
        $item2Name = $_POST['item2Name'];
        $item3Name = $_POST['item3Name'];
        $item4Name = $_POST['item4Name'];
        $item5Name = $_POST['item5Name'];
        $item6Name = $_POST['item6Name'];
        $item7Name = $_POST['item7Name'];
        $item8Name = $_POST['item8Name'];
        $item9Name = $_POST['item9Name'];
        $item10Name = $_POST['item10Name'];

        $item1Rate = $_POST['item1Rate'];
        $item2Rate = $_POST['item2Rate'];
        $item3Rate = $_POST['item3Rate'];
        $item4Rate = $_POST['item4Rate'];
        $item5Rate = $_POST['item5Rate'];
        $item6Rate = $_POST['item6Rate'];
        $item7Rate = $_POST['item7Rate'];
        $item8Rate = $_POST['item8Rate'];
        $item9Rate = $_POST['item9Rate'];
        $item10Rate = $_POST['item10Rate'];

        $discount = $_POST['discount'];
        $taxRate = $_POST['taxRate'];
        $notes = $_POST['notes'];

        // Calculate line item totals
        $item1Total = $item1Qty * $item1Rate;
        $item2Total = $item2Qty * $item2Rate;
        $item3Total = $item3Qty * $item3Rate;
        $item4Total = $item4Qty * $item4Rate;
        $item5Total = $item5Qty * $item5Rate;
        $item6Total = $item6Qty * $item6Rate;
        $item7Total = $item7Qty * $item7Rate;
        $item8Total = $item8Qty * $item8Rate;
        $item9Total = $item9Qty * $item9Rate;
        $item10Total = $item10Qty * $item10Rate;


        // Calculate Subtotal
        $subTotal = 0;
        $subTotal += $item1Total;
        $subTotal += $item2Total;
        $subTotal += $item3Total;
        $subTotal += $item4Total;
        $subTotal += $item5Total;
        $subTotal += $item6Total;
        $subTotal += $item7Total;
        $subTotal += $item8Total;
        $subTotal += $item9Total;
        $subTotal += $item10Total;

        // Calculate Subtotal after discount is applied
        $discountedSubTotal = $subTotal - $discount;

        // Convert tax rate from percent to decimal
        $taxRate = $taxRate / 100;
        // Calculate the tax amount
        $tax = $discountedSubTotal * $taxRate;

        // Calculate the total price
        $total = $discountedSubTotal + $tax;

        // Require config.inc.php for configuration variables
        require_once "config.inc.php";

        // Connect to MySQL
        mysql_connect($path, $username, $password);
        // Select the database, if error die with error message
        @mysql_select_db($database) or die("Unable to select database.<br />" . mysql_error());

        // Define MySQL query to update the invoice record
        $query = "UPDATE tblInvoices SET customerID='$customerID', date='$date', item1Qty='$item1Qty', item1Name='$item1Name', item1Rate='$item1Rate', item1Total='$item1Total', item2Qty='$item2Qty', item2Name='$item2Name', item2Rate='$item2Rate', item2Total='$item2Total', item3Qty='$item3Qty', item3Name='$item3Name', item3Rate='$item3Rate', item3Total='$item3Total', item4Qty='$item4Qty', item4Name='$item4Name', item4Rate='$item4Rate', item4Total='$item4Total', item5Qty='$item5Qty', item5Name='$item5Name', item5Rate='$item5Rate', item5Total='$item5Total', item6Qty='$item6Qty', item6Name='$item6Name', item6Rate='$item6Rate', item6Total='$item6Total', item7Qty='$item7Qty', item7Name='$item7Name', item7Rate='$item7Rate', item7Total='$item7Total', item8Qty='$item8Qty', item8Name='$item8Name', item8Rate='$item8Rate', item8Total='$item8Total', item9Qty='$item9Qty', item9Name='$item9Name', item9Rate='$item9Rate', item9Total='$item9Total', item10Qty='$item10Qty', item10Name='$item10Name', item10Rate='$item10Rate', item10Total='$item10Total', subTotal='$subTotal', discount='$discount', tax='$tax', total='$total', notes='$notes' WHERE `invoiceNum`=$invoiceNum";

        // Execute the MySQL query, if error die with error message
        mysql_query($query) or die('<p style="color: red;">Update Failed.</p>' . mysql_error());

        // Close the connection
        mysql_close();

        // Output Success message and redirect to invoices.php
        echo '<html>';
        echo '<head>';
            echo '<meta http-equiv="refresh" content="0;url=invoices.php" />';
        echo '</head>';

        echo '<body>';
            echo 'Update Successful';
        echo '</body>';
        echo '</html>';

    // If invoiceNum not passed by POST...
    } else {
        // Die with error message
        die('<p style="color: red;">Error: Please provide the Invoice Number of the invoice to be updated.</p>');
    }
?>
